<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!--<link rel="stylesheet" type="text/css" href="common/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="common/bootstrap/css/bootstrap.css" media="all">
    <link rel="stylesheet" type="text/css" href="common/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="css/personal.css" media="all">-->

    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/cba/style.css/}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/cba/my.css/}" media="all">
    <style>
        .childrenBody{
            padding: 30px;
        }
    </style>
</head>
<body class="childrenBody">
<form class="layui-form html_save" lay-filter="form">

    <input name="id" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input class="layui-input" name="name" lay-verify="required" placeholder="请输入名称" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" checked lay-skin="switch" lay-text="开启|暂停" id="status">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单</label>
        <div class="layui-input-block" id="menu">
            <table class="layui-table" lay-size="sm" th:each="menu:${menuList}">
                <thead>
                <tr>
                    <th class="menuFirst"><input type="checkbox" lay-skin="primary" lay-filter="menu"
                                                 th:value="${menu.id}" th:ref="${menu.id}" th:title="${menu.text}"
                                                 th:checked="${menu.choose}"></th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="m:${menu.subset}">
                    <td style="padding-left: 40px" class="menuSecond"><input type="checkbox" lay-skin="primary"
                                                                             th:value="${m.id}" lay-filter="menu"
                                                                             th:title="${m.text}"
                                                                             th:checked="${m.choose}">
                    </td>
                </tr>

                </tbody>
            </table>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入备注" class="layui-textarea" name="memo"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveContainer">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script th:src="@{/js/layui/layui.js}"></script>
<script th:src="@{/js/cba/login-timeout.js}"></script>
<!--<script type="text/javascript" src="js/newsadd.js"></script>-->

<script type="text/javascript" th:inline="javascript">
    layui.use(['table', 'form', "layer"], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , $ = layui.jquery;

        var user = [[${role}]];
        if (user != null) {
            form.val("form", {
                "name": user.name
                , "memo": user.memo
                , "id": user.id
            });

            if (user.status == 1) {
                $("#status").removeAttr("checked");
            }
            form.render("checkbox"); //刷新select选择框渲染

        }

        form.verify({

        });

        //监听提交
        form.on('submit(saveContainer)', function (data) {

            data.field.status = data.field.status == "on" ? 0 : 1;

            var mArr = [];
            $("#menu .layui-form-checked").each(function (i, val) {
                mArr.push($(val).prev().val());
            });
            data.field.menus = mArr.join(",");
            $.ajax({
                url: [[${basePath}]] + "/role/save", data: data.field, sync: "post", success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.href = [[${basePath}]] + "/role/index";
                            parent.layer.close(index);
                        }, 1500);
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });

            return false;
        });


        form.on('checkbox(menu)', function (data) {
            //console.log(data.elem); //得到checkbox原始DOM对象
            //console.log(data.elem.checked); //是否被选中，true或者false
            //console.log(data.value); //复选框value值，也可以通过data.elem.value得到
            //console.log(data.othis); //得到美化后的DOM对象

            if (data.elem.checked) {
                if ($(data.othis).parent().hasClass("menuFirst")) {//父级菜单
                    //所有子菜单都选中
                    $(data.othis).parents("thead").next().find("div").addClass("layui-form-checked");
                } else {//子菜单
                    $(data.othis).parents("tbody").prev().find("div").addClass("layui-form-checked");
                }

            } else {
                if ($(data.othis).parent().hasClass("menuFirst")) {//父级菜单
                    //所有子菜单都选中
                    $(data.othis).parents("thead").next().find("div").removeClass("layui-form-checked");
                } else {//子菜单
                    if (!$(data.othis).parents("tbody").find("div").hasClass("layui-form-checked")) {
                        $(data.othis).parents("tbody").prev().find("div").removeClass("layui-form-checked");
                    }
                }
            }
        });


    });
</script>
</body>
</html>